package com.eleventwelve.iris.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.opentides.service.impl.BaseCrudServiceImpl;

import com.eleventwelve.iris.entity.Patient;
import com.eleventwelve.iris.entity.PatientDisease;
import com.eleventwelve.iris.service.PatientDiseaseService;

public class PatientDiseaseServiceImpl extends BaseCrudServiceImpl<PatientDisease> implements PatientDiseaseService{

	@Override
	public void savePatientDisease(Patient patient) {
		if(patient.getPatientDiseases()!=null && patient.getPatientDiseases().size()>0){
			List<PatientDisease> patientDiseases = patient.getPatientDiseases();
			List<PatientDisease> toDelete = new ArrayList<PatientDisease>();
			
			for (PatientDisease patientDisease : patient.getPatientDiseases()) {
				if(!patientDisease.getIsDeleted() && patientDisease.getDisease()!=null){
					patientDisease.setPatient(patient);
				} else {
					toDelete.add(patientDisease);
				}
			}
			
			patientDiseases.removeAll(toDelete);
			for (PatientDisease patientDisease : toDelete) {
				if(patientDisease.getId()!=null){
					delete(patientDisease.getId());
				}
			}
			
			getDao().saveAllEntityModel(patientDiseases);
		}
		
	}

	@Override
	public void saveAll(List<PatientDisease> diseases) {
		getDao().saveAllEntityModel(diseases);
	}

}
